/*
 * RecordLeftComparer.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.dictionary.records;


import edu.sdsu.util.Comparer;

public class RecordBothSidesComparer extends RecordComparer implements
		Comparer<LeftRightValuesRecord> {

	@Override
	protected RecordRankingValues getRank(LeftRightValuesRecord record) {
		RecordRankingValues leftRank = record.getLeftRank();
		RecordRankingValues rightRank = record.getRightRank();
		
		int result = RecordRankingValues.comparator.compare( leftRank, rightRank );
		if ( result == RecordRankingValues.LESS_THAN )
			return leftRank;
		else if ( result == RecordRankingValues.GREATER_THAN)
			return rightRank;
		
		if ( leftRank.index == RecordRankingValues.INFINITE_VALUE )
			return rightRank;
		else if ( rightRank.index == RecordRankingValues.INFINITE_VALUE )
			return leftRank;
		
		String leftValue = record.getLeftNthValue( leftRank.index );
		String rightValue = record.getRightNthValue( rightRank.index );
		if ( leftValue.length() < rightValue.length())
			return leftRank;
		else
			return rightRank;
		
	}

//	@Override
//	protected int getSize(LeftRightValuesRecord record) {
//		return Math
//				.min(record.getLeftValuesSize(), record.getRightValuesSize());
//	}
	
	@Override
	protected String getValue(LeftRightValuesRecord record, int position ) {
		RecordRankingValues leftRank = record.getLeftRank();
		RecordRankingValues rightRank = record.getRightRank();
		if ( leftRank.index < rightRank.index )
			return record.getLeftNthValue( position );
		else
			return record.getRightNthValue( position );
		
	}

}
